/*
 * 작성일 : 2014. 4. 10.
 * 작성자 : hch1982
 *
 * 설명
*/ 
/**
 * 
 */
package springbook.temp;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import org.springframework.util.ClassUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AbstractRefreshableWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author hch1982
 *
 * 코드 수정 히스토리
 * 날짜		작업자		태그
 * 2014. 4. 10.	hch1982	
 */
public class ConfigureDispatcherServlet extends DispatcherServlet{
	private Class<?>[] classes;
	private String[] locations;
	
	private ModelAndView modelAndView;
	
	public ConfigureDispatcherServlet(String[] locations){
		this.locations = locations;
	}
	
	public ConfigureDispatcherServlet(Class<?> ...classes){
		this.classes = classes;
	}
	
	public void setLocatinos(String ...locations){
		this.locations = locations;
	}
	
	public void setRelativeLocations(Class clazz, String ...relativeLocations){
		String [] locations = new String[relativeLocations.length];
		String currentPath = ClassUtils.classPackageAsResourcePath(clazz) + "/";
		
		for(int i = 0; i<relativeLocations.length; i++){
			locations[i] = currentPath + relativeLocations[i];
		}
		
		this.setLocatinos(locations);
	}
	
	public void setClasses(Class<?> ...classes){
		this.classes = classes;
	}
	
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		modelAndView = null;
		super.service(req, res);
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.FrameworkServlet#createWebApplicationContext(org.springframework.context.ApplicationContext)
	 */
	@Override
	protected WebApplicationContext createWebApplicationContext(
			ApplicationContext parent) {
		// TODO Auto-generated method stub
		AbstractRefreshableWebApplicationContext wac = new AbstractRefreshableWebApplicationContext() {
			
			@Override
			protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
					throws BeansException, IOException {
				// TODO Auto-generated method stub
				if(locations!=null){
					XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(beanFactory);
					xmlReader.loadBeanDefinitions(locations);
				}
				
				if(classes!=null){
					AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(beanFactory);
					reader.register(classes);
				}
			}
		};
		
		wac.setServletContext(getServletContext());
		wac.setServletConfig(getServletConfig());
		wac.refresh();
		
		return wac;
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.DispatcherServlet#render(org.springframework.web.servlet.ModelAndView, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void render(ModelAndView mv, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		this.modelAndView = mv;
		super.render(mv, request, response);
	}
	
	public ModelAndView getModelAndView(){
		return modelAndView;
	}
	
	
	
	
}
